package de.hft.st.algodat.ctt.model;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Vector;

public class Problem {

	private String filename;
	private String name;
	private int days;
	private int periods_per_day;
	private int daily_lectures_min;
	private int daily_lectures_max;

	private List<Course> courses = new ArrayList<Course>();
	private List<Room> rooms = new ArrayList<Room>();
	private List<Curriculum> curricula = new ArrayList<Curriculum>();
	private List<ConstraintPeriod> constraintsPeriod = new ArrayList<ConstraintPeriod>();
	private List<ConstraintRoom> constraintsRoom = new ArrayList<ConstraintRoom>();

	public Problem() {
	}

	public String toString() {
		StringBuffer toStr = new StringBuffer();
		toStr.append("Filename         : " + filename);
		toStr.append("\nName             : " + name);
		toStr.append("\nDays             : " + days);
		toStr.append("\nPeriods p. Day   : " + periods_per_day);
		toStr.append("\nDaily Lec. Min   : " + daily_lectures_min);
		toStr.append("\nDaily Lec. Max   : " + daily_lectures_max);
		toStr.append("\n# of df. Courses : " + courses.size());
		toStr.append("\n# of Courses     : " + getNumberOfCoursesInCurricula());
		toStr.append("\n# of Rooms       : " + rooms.size());
		toStr.append("\n# of Curricula   : " + curricula.size());
		toStr.append("\n# of Constraints : "
				+ (constraintsPeriod.size() + constraintsRoom.size()) + "\n");
		toStr.append("== Courses:\n");
		for (Iterator<Course> itr = courses.iterator(); itr.hasNext();) {
			toStr.append(itr.next().toString());
		}
		toStr.append("== Rooms:\n");
		for (Iterator<Room> itr = rooms.iterator(); itr.hasNext();) {
			toStr.append(itr.next().toString());
		}
		toStr.append("== Curricula:\n");
		for (Iterator<Curriculum> itr = curricula.iterator(); itr.hasNext();) {
			toStr.append(itr.next().toString());
		}
		toStr.append("== Constraints Period: (" + constraintsPeriod.size()
				+ ")\n");
		for (Iterator<ConstraintPeriod> itr = constraintsPeriod.iterator(); itr
				.hasNext();) {
			toStr.append(itr.next().toString());
		}
		toStr.append("== Constraints Room: (" + constraintsRoom.size() + ")\n");
		for (Iterator<ConstraintRoom> itr = constraintsRoom.iterator(); itr
				.hasNext();) {
			toStr.append(itr.next().toString());
		}
		return toStr.toString();
	}

	public int getNumberOfCoursesInCurricula() {
		int sumOf = 0;
		for (Iterator<Curriculum> itr = curricula.iterator(); itr.hasNext();) {
			sumOf += itr.next().getNumberOfCourses();
		}
		return sumOf;
//		int sumOf = 0;
//		for (Iterator<Curriculum> itr = curricula.iterator(); itr.hasNext();) {
//			List<Course> worker = itr.next().getCList();
//			for(Iterator<Course> cItr = worker.iterator(); cItr.hasNext();) {
//				sumOf += cItr.next().getLectures();
//			}
//		}
//		return sumOf;
	}

	public void addRoom(String id, int size, int building) {
		rooms.add(new Room(id, size, building));
	}

	public void addCourse(String id, String teacher, int lectures,
			int min_days, int students, String double_lectures) {
		courses.add(new Course(id, teacher, lectures, min_days, students,
				double_lectures));
	}

	public void addCurriculum(String id, Vector<String> currCourses, List<Course> cList) {
		curricula.add(new Curriculum(id, currCourses,cList));
	}

	public String getFilename() {
		return filename;
	}

	public int getNumberOfRooms() {
		return rooms.size();
	}

	public void addConstraintPeriod(String course, List<String> days,
			List<String> periods) {
		constraintsPeriod.add(new ConstraintPeriod(course, days, periods));
	}

	public void addConstraintRoom(String course, List<String> rooms) {
		constraintsRoom.add(new ConstraintRoom(course, rooms));
	}

	public void setFilename(String filename) {
		this.filename = filename;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public int getDays() {
		return days;
	}

	public void setDays(int days) {
		this.days = days;
	}

	public int getPeriods_per_day() {
		return periods_per_day;
	}

	public void setPeriods_per_day(int periods_per_day) {
		this.periods_per_day = periods_per_day;
	}

	public int getDaily_lectures_min() {
		return daily_lectures_min;
	}

	public void setDaily_lectures_min(int daily_lectures_min) {
		this.daily_lectures_min = daily_lectures_min;
	}

	public int getDaily_lectures_max() {
		return daily_lectures_max;
	}

	public void setDaily_lectures_max(int daily_lectures_max) {
		this.daily_lectures_max = daily_lectures_max;
	}

	public List<Course> getCourses() {
		return courses;
	}

	public Course getCourse(String ref) {
		Course refCourse = null;
		for (Iterator<Course> itr = courses.iterator(); itr.hasNext();) {
			Course worker = itr.next();
			if (worker.getId().equals(ref)) {
				refCourse = worker;
				break;
			}
		}
		return refCourse; 
	}

	public int getNumberOfCourses() {
		return this.courses.size();
	}

	public int getNumberOfTimeslots() {
		return (this.days * this.periods_per_day);
	}
	
	public List<Curriculum> getCurricula() {
		return this.curricula;
	}

	public Curriculum getCurriculumByID(String cID) {
		Curriculum rVal = null;
		for (Iterator<Curriculum> itr = this.curricula.iterator(); itr.hasNext();) {
			Curriculum worker = itr.next();
			if(worker.getId().equals(cID)) {
				rVal = worker;
			}
		}	
		return rVal;
	}
	
	public List<Room> getRooms() {
		return this.rooms;
	}
	
	public List<ConstraintPeriod> getConstraintsPeriod() {
		return this.constraintsPeriod;
	}

	public List<ConstraintRoom> getConstraintsRoom() {
		return this.constraintsRoom;
	}

	public List<Curriculum> getCurriculaWithSameCourse(String currID, String courseID) {
		List<Curriculum> rVal = new ArrayList<Curriculum>();
		for (Iterator<Curriculum> itr = this.curricula.iterator(); itr.hasNext();) {
			Curriculum worker = itr.next();
			if(!worker.getId().equals(currID) && worker.containsCourseByRef(courseID)) {
				rVal.add(worker);
			}
		}	
//		System.out.println("returning " + rVal.size() + " curricula with same course");
		return rVal;
	}

	public List<ConstraintPeriod> getConstraintsPeriodForCourse(Course course) {
		List<ConstraintPeriod> rVal = new ArrayList<ConstraintPeriod>();
		for(Iterator<ConstraintPeriod> cpItr = this.constraintsPeriod.iterator(); cpItr.hasNext();) {
			ConstraintPeriod worker = cpItr.next();
			if(worker.getCourse().equals(course.getId())) {
				rVal.add(worker);
			}
		}
		return rVal;
	}
}
